package com.zzz.service.impl;

import com.aaa.entity.UCollection;
import com.aaa.security.util.WebUtils;
import com.aaa.util.JwtUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzz.service.UCollectionService;
import com.zzz.mapper.UCollectionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author 86176
 * @description 针对表【u_collection】的数据库操作Service实现
 * @createDate 2023-03-09 11:12:57
 */
@Service
@SuppressWarnings("all")
public class UCollectionServiceImpl extends ServiceImpl<UCollectionMapper, UCollection>
        implements UCollectionService {

    @Autowired
    private UCollectionMapper uCollectionMapper;

    //查询商品是否已经收藏
    @Override
    public Result isGoodsCollection(String goodsid) {
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        Object username1 = tokenChaim.get("username");
        String username = username1.toString();
        List<UCollection> goodsCollection = uCollectionMapper.isGoodsCollection(goodsid, username);
        return new Result(2000, "查询成功", goodsCollection);
    }

    //查询店铺是否已经关注
    @Override
    public Result isCollectionBusiness(String bid) {
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        Object username1 = tokenChaim.get("username");
        String username = username1.toString();
        List<UCollection> collectionBusiness = uCollectionMapper.isCollectionBusiness(bid, username);
        return new Result(2000, "查询成功", collectionBusiness);
    }

    //收藏或取消收藏状态
    @Override
    public Result changeCollectionStatus(UCollection uCollection) {
        QueryWrapper<UCollection> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uCollection.getUid());
        wrapper.eq("goodsid", uCollection.getGoodsid());
        UCollection uCollection1 = uCollectionMapper.selectOne(wrapper);
        if (uCollection1 != null) {
            Integer integer = uCollectionMapper.changeCollectionStatus(uCollection);
            if (integer > 0) {
                return new Result(2000, "修改成功", uCollection.getIsDeleted());
            } else {
                return new Result(5000, "修改失败", uCollection.getIsDeleted());
            }
        } else {
            String s = UUID.randomUUID().toString();
            uCollection.setId(s);
            uCollection.setIsDeleted(0);
            if (uCollection.getGoodsid() != null) {
                uCollection.setStatus(1);
            } else {
                return new Result(5000, "status未传入");
            }
            int insert = uCollectionMapper.insert(uCollection);
            if (insert > 0) {
                return new Result(2000, "收藏成功", 0);
            } else {
                return new Result(5000, "收藏失败", 1);
            }
        }
    }

    //关注或取关店铺
    @Override
    public Result changeGuanzhu(UCollection uCollection) {
        QueryWrapper<UCollection> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uCollection.getUid());
        wrapper.eq("bid", uCollection.getBid());
        UCollection uCollection2 = uCollectionMapper.selectOne(wrapper);
        if (uCollection2 != null) {
            Integer integer = uCollectionMapper.changeGuanzhu(uCollection);
            if (integer > 0) {
                return new Result(2000, "修改成功", uCollection.getIsDeleted());
            } else {
                return new Result(5000, "修改失败", uCollection.getIsDeleted());
            }
        } else {
            String s1 = UUID.randomUUID().toString();
            uCollection.setId(s1);
            uCollection.setIsDeleted(0);
            if (uCollection.getBid() != null) {
                uCollection.setStatus(0);
            } else {
                return new Result(5000, "status未传入");
            }
            int insert = uCollectionMapper.insert(uCollection);
            if (insert > 0) {
                return new Result(2000, "关注成功", 0);
            } else {
                return new Result(5000, "关注失败", 1);
            }
        }
    }
}







